;---------------------------------------------------------------------
;
; File: example_c.sexp
; Examples that demonstrate the main features of SIF02.
;
; Copyright (C) 2006-2008 by Ariel Ortiz <ariel.ortiz@itesm.mx>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;---------------------------------------------------------------------

(define cons-end
  (fn (x lst)
    (if (null? lst)
        (cons x ())
        (cons (first lst) (cons-end x (rest lst))))))

(define binary
  (fn (n)
    (if (= n 0)
        ()
        (cons-end (% n 2) (binary (/ n 2))))))

(define numbers (quote (1 2 3 4 5)))
(define foo (cons (quote +) numbers))

(define bar ((fn (x) (fn (y) (+ x y))) 1))
(define baz ((fn (x) (dyfn (y) (+ x y))) 1))
(define x 10)

                   ; Displayed results:
                   ;
(binary 0)         ;         ()
(binary 30)        ;         (1 1 1 1 0)
(binary 129)       ;         (1 0 0 0 0 0 0 1)
(apply * numbers)  ;         120
foo                ;         (+ 1 2 3 4 5)
(eval foo)         ;         15
(procedure? *)     ;         t
(procedure? foo)   ;         ()
(procedure? bar)   ;         t
(procedure? baz)   ;         t
(bar 1)            ;         2
(baz 1)            ;         11